import {RouteRecordRaw} from "vue-router";
import {defineStore} from "pinia";
// import router, {constantRoutes} from "@/router";
// import {store} from "@/store";
// import {listRoutes} from "@/api/menu";


// 匹配views里面所有的.vue文件
const modules = import.meta.glob("../../views/**/**.vue");
// const Layout = () => import("@/layout/index.vue");

/**
 * Use meta.role to determine if the current user has permission
 *
 * @param roles 用户角色集合
 * @param route 路由
 * @returns
 */

const hasPermission = (roles: string[], route: RouteRecordRaw) => {
    if (route.meta && route.meta.roles) {
        if (roles.includes("ROOT")) {
            return true
        }
        return roles.some((role)=>{
            if(route.meta?.roles){
                return route.meta.roles.includes(role);
            }
        })
    }
    return false
}

/**
 * 递归过滤有权限的异步(动态)路由
 *
 * @param routes 接口返回的异步(动态)路由
 * @param roles 用户角色集合
 * @returns 返回用户有权限的异步(动态)路由
 */

const filterAsyncRoutes = (routes:RouteRecordRaw[],roles:string[])=>{
    const asyncRoutes: RouteRecordRaw[] = [];
    routes.forEach((route)=>{
        const tmpRoute = {...route}
        if (!route.name) {
            tmpRoute.name = route.path;
        }
        // 判断用户(角色)是否有该路由的访问权限
        if(hasPermission(roles,tmpRoute)){

        }
    })

}

